package com.dab.springboot.generator.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dab.springboot.generator.common.Constants;
import com.dab.springboot.generator.common.CustomException;
import com.dab.springboot.generator.domain.User;
import com.dab.springboot.generator.dto.UserDto;
import com.dab.springboot.generator.service.UserService;
import com.dab.springboot.generator.mapper.UserMapper;
import lombok.extern.java.Log;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * @author DABB
 * @description 针对表【sys_user】的数据库操作Service实现
 * @createDate 2023-04-11 20:27:38
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public UserDto login(UserDto userDto) {
        //构造查询器
        LambdaQueryWrapper<User> userLqw = new LambdaQueryWrapper<>();
        //条件查询器
        userLqw.eq(User::getUsername, userDto.username);
        userLqw.eq(User::getPassword, userDto.password);
        User one;

        try {
            one = this.getOne(userLqw);
        } catch (Exception e) {
            log.error("{}", e);
            throw new CustomException(Constants.C0DE_500 + "系统错误");
        }

        if (one != null) {
            //数据库数据载入Dto
            BeanUtils.copyProperties(one, userDto);
            return userDto;
        } else {
            throw new CustomException(Constants.C0DE_000 + "用户名或密码错误");
        }
    }
}




